package com.fzt.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzt.reggie.dto.SetmealDto;
import com.fzt.reggie.entity.Setmeal;
import com.fzt.reggie.entity.SetmealDish;
import com.fzt.reggie.mapper.SetMealMapper;
import com.fzt.reggie.service.SetMealService;
import com.fzt.reggie.service.SetmealDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        /*
        新增套餐，同时保存套餐和菜品的关联关系
         */
        //在套餐表中新增套餐
        this.save(setmealDto);//执行完保存操作，框架会给setmealDto赋值


        //在setmeal_dish中添加数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //对setSetmealId赋值
        setmealDishes.stream().map((item) ->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
}
